package com.gryffonx.NameGenerators;

import com.gryffonx.DiceRoller.StaticDiceRoller;
import com.gryffonx.utils.StringUtils;
import com.gryffonx.utils.XMLUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.net.MalformedURLException;


/**
 * @author Erik Stell
 * @version 1.0
 */
public class EmpyrealNameGenerator {

    private static final File fileToBeParsed = new File("src/com/gryffonx/xml/empyric.xml");
    private static String gender = "";
    private static final SAXReader reader = new SAXReader();
    private static Document xmlDoc = null;

    /**
     * @return
     */
    private static String getGender() {
        return gender;
    }

    /**
     * @param gender
     */
    private static void setGender(String gender) {
        EmpyrealNameGenerator.gender = gender;
    }

    public static String generateSurname(){
        try {
            xmlDoc = reader.read(fileToBeParsed);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        StringBuffer retVal = new StringBuffer();
        retVal.append(StringUtils.capitalize(XMLUtils.getNodeValue("surname", xmlDoc, 1)));
        retVal.append("'");
        retVal.append(getPrefix(determineGroup()));

        return retVal.toString();
    }

    /**
     * @param gender
     * @param howMany
     * @return
     */
    public static String generateName(String gender, int howMany, boolean addTitle, boolean addSurname) throws Exception {
        if (howMany <= 0) {
            howMany = 1;
        }
        try {
            xmlDoc = reader.read(fileToBeParsed);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        setGender(gender);
        StringBuffer retVal = new StringBuffer();

        for (int i = 0; i < howMany; i++) {
            StringBuffer finishedName = new StringBuffer();

            try {
                finishedName.append(getPrefix(determineGroup()));
                finishedName.append(extractSuffix());
            } catch (Exception e) {
                e.printStackTrace();
            }

            retVal.append(StringUtils.capitalize(finishedName.toString()));
            if (addTitle){
                retVal.append(TitleGenerator.generateName(1));
            }
            if (addSurname){
                retVal.append(" ").append(generateSurname());
            }
        }

        return retVal.toString();
    }

    /**
     * @param groupName
     * @return
     */
    private static String getPrefix(String groupName) {
        String retVal = "";

        try {
            retVal = StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, Integer.parseInt(groupName)));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return retVal;
    }

    /**
     * @return
     */
    private static String determineGroup() {
        return String.valueOf(StaticDiceRoller.rolldSomething(6, 1));
    }

    /**
     * @return
     */
    private static String extractSuffix() {
        String retVal = "";
        try {
            retVal = XMLUtils.getNodeValue(xmlDoc, getGender().toLowerCase());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return retVal;
    }
}
